package com.soft.servlet;

import com.soft.dao.AdministratorsDAO;
import com.soft.entity.Administrators;
import com.soft.util.PageBean;

import java.io.IOException;
import java.sql.SQLException;
import java.util.List;
import java.util.Map;
import javax.servlet.ServletException;
import javax.servlet.ServletRequest;
import javax.servlet.ServletResponse;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

//管理员
@WebServlet({"/AdministratorsServlet"})
public class AdministratorsServlet extends HttpServlet {

    private static final long serialVersionUID = 1L;
    //跳转uri
    private String uri;

    public AdministratorsServlet() {
        super();
    }

    public void init() throws ServletException {

    }



    /**
     * @see HttpServlet#doGet(HttpServletRequest req, HttpServletResponse resp)
     */
    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp)
            throws ServletException, IOException {
        // 获取请求数据
        req.setCharacterEncoding("UTF-8");
        resp.setContentType("text/html;charset=UTF-8");

        // 获取操作类型
        String method = req.getParameter("method");

        if ("add".equals(method)) {
            add(req, resp);
        } else if ("list".equals(method)) {
            list(req, resp);
        } else if ("search".equals(method)) {
            search(req, resp);
        } else if ("delete".equals(method)) {
            delete(req, resp);
        } else if ("Jumpup".equals(method)) {
            Jumpup(req, resp);
        } else if ("update".equals(method)) {
            update(req, resp);
        }

    }

    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp)
            throws ServletException, IOException {
        doGet(req, resp);
    }

    /**
     * 一览表取得
     * @param req
     * @param resp
     */
    public void list(HttpServletRequest req, HttpServletResponse resp)
            throws ServletException, IOException {
        try {
            //1. 获取“当前页”参数；（当第一次访问当前页为null）
            String currPage = req.getParameter("currentPage");
            //判断
            if (currPage == null || "".equals(currPage.trim())) {
                currPage = "1";       //第一次访问，设置当前页为1；
            }
            //转换
            int currentPage = Integer.parseInt(currPage);

            //2. 创建dao对象，设置当前页参数；
            PageBean pageBean = new PageBean();
            pageBean.setCurrentPage(currentPage);
            AdministratorsDAO dao = new AdministratorsDAO();
            // 调用dao对象的getAll方法，得到结果
            Map<String, Object> result = dao.getAll(pageBean);

            //保存
            req.setAttribute("result", result);

            // 跳转页
            uri = "/sys/administrators/administrators.jsp";
        } catch (NumberFormatException e) {
            e.printStackTrace();
        } catch (Exception e) {
            e.printStackTrace();
        }/* 转发*/
        try {
            req.getRequestDispatcher(uri).forward(req, resp);
        } catch (ServletException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    //添加管理员用户
    public void add(HttpServletRequest req,HttpServletResponse resp) throws ServletException, IOException {
        try {
            String administratorsName = req.getParameter("name");
            String administratorsPassword = req.getParameter("password");

            Administrators administrators = new Administrators();
            administrators.setName(administratorsName);
            administrators.setPassword(administratorsPassword);

            AdministratorsDAO dao = new AdministratorsDAO();
            dao.add(administrators);

            this.uri = "/AdministratorsServlet?method=list";
        } catch (SQLException e) {
            e.printStackTrace();
        }

        //转发
        req.getRequestDispatcher(this.uri)
                .forward((ServletRequest) req,(ServletResponse) resp);
    }

        //删除管理员用户
        public void delete(HttpServletRequest req, HttpServletResponse resp)
                throws ServletException, IOException {
            try {
                int administratorsId = Integer.parseInt(req.getParameter("id"));

                Administrators cc = new Administrators();
                cc.setId(administratorsId);

                AdministratorsDAO dao = new AdministratorsDAO();
                dao.delete(cc);

                this.uri = "/AdministratorsServlet?method=list";
            } catch (Exception e) {
                e.printStackTrace();
            }

            req.getRequestDispatcher(this.uri)
                    .forward((ServletRequest) req, (ServletResponse) resp);
        }
        //转发到修改页面
        public void Jumpup(HttpServletRequest req, HttpServletResponse resp)
                throws ServletException, IOException {
            try {
                int administratorsId = Integer.parseInt(req.getParameter("id"));

                Administrators administrators = new Administrators();
                administrators.setId(administratorsId);

                AdministratorsDAO dao = new AdministratorsDAO();
                Administrators result = dao.getone(administrators);
                //req.setAttribute是在请求域里面加了一个请求的参数，所以在sendRedirect以后是无法取到req.setAttribute的请求的。
                req.setAttribute("listClass", result);

                this.uri = "/sys/administrators/update.jsp";
            } catch (NumberFormatException e) {
                e.printStackTrace();
            }
            req.getRequestDispatcher(this.uri)
                    .forward((ServletRequest) req, (ServletResponse) resp);
        }
        //修改管理员用户
        public void update(HttpServletRequest req, HttpServletResponse resp)
                throws ServletException, IOException {
            try {
                int administratorsId = Integer.parseInt(req.getParameter("id"));
                String administratorsName = req.getParameter("name");
                String administratorsPassword = req.getParameter("password");

                Administrators administrators = new Administrators();
                administrators.setId(administratorsId);
                administrators.setName(administratorsName);
                administrators.setPassword(administratorsPassword);

                AdministratorsDAO dao = new AdministratorsDAO();
                dao.update(administrators);

                this.uri = "/AdministratorsServlet?method=list";
            } catch (NumberFormatException | SQLException e) {
                e.printStackTrace();
            }
            req.getRequestDispatcher(this.uri)
                    .forward((ServletRequest) req, (ServletResponse) resp);
        }

        //查询管理员用户
        public void search(HttpServletRequest req, HttpServletResponse resp)
                throws ServletException, IOException {
            try {
                String currPage = req.getParameter("currentPage");

                String administratorsName = req.getParameter("name");

                if (currPage == null || "".equals(currPage.trim())) {
                    currPage = "1";
                }

                int currentPage = Integer.parseInt(currPage);

                PageBean pageBean = new PageBean();
                pageBean.setCurrentPage(currentPage);

                Administrators title = new Administrators();
                title.setName(administratorsName);

                AdministratorsDAO dao = new AdministratorsDAO();
                Map<String, Object> result = dao.search(pageBean, title);

                req.setAttribute("result",result);

                this.uri = "/sys/administrators/administrators.jsp";
            } catch (Exception e) {
                e.printStackTrace();
            }

            req.getRequestDispatcher(this.uri)
                    .forward((ServletRequest) req, (ServletResponse) resp);
        }

}